package com.siping.test;

import org.apache.commons.lang.math.RandomUtils;

import java.io.*;

/**
 * @author Yang Xu
 * @date 2021/9/6 16:22
 * @description: 构建测试数据, 生成大量关键词
 */
public class BuildTestData {
    public static final int TOTAL = 10000000;
    public static final String WRITE_FILE = "D:\\tmp\\test\\data\\words3.txt";

    public static void main(String[] args) throws IOException {
        /**
         * 1.读取词库到数组中
         */
        String thuocl = "D:\\tmp\\test\\THUOCL_it.txt";
        InputStream is = new FileInputStream(thuocl);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String[] thuoclArr = new String[16000];
        String line = reader.readLine(); // 读取第一行
        int i = 0;
        while (line != null) {
            String[] split = line.split("\\t");
            if (split.length >= 1) {
                thuoclArr[i] = split[0];
            }
            line = reader.readLine();
            i++;
        }
        System.out.println("读取词库成功，共计：" + thuoclArr.length);
        reader.close();
        is.close();
        /**
         * 2.随机从词库中选择单词写入到文件中
         */
        long begin = System.currentTimeMillis();
        ConsoleProgressBar cpb = new ConsoleProgressBar(50, '#');
        FileWriter fileWriter = new FileWriter(new File(WRITE_FILE), true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        for (int j = 0; j < TOTAL; j++) {
            int n = RandomUtils.nextInt(thuoclArr.length);
            bufferedWriter.write(thuoclArr[n] + "\r\n");
            if (j % (TOTAL / 100) == 0) {
                cpb.show(j * 100 / TOTAL);
            }
        }
        System.out.println("生成测试数据完毕，耗时：" + (System.currentTimeMillis() - begin) + "ms");
        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();

    }
}
